home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-03-06 | 3.0 KB | 87 lines | [TEXT/GEOL] |
- Item 2415334 23-Jan-91 13:07PST
-
- From: V0629 New England Digital,R & D,ASC,VAR
-
- To: D4682 Marx, Peter,PRT
- CPLUS.APPLE$ C++ Interest List--Apple Employees
- CPLUS.DEV$ C++ Interest List--Developers
-
- Item forwarded by BEISEL to WANG2
-
- ------------------------------------------------------------------------------
-
- Sub: RE>>const vs #define in C
-
- Attn: Marx, Peter,PRT
- Attn: cplusa
- Attn: CPLUS
- SentBy: Karim Chichakly
- Date 1/23/91
- Subject RE>>const vs #define in CFr
- From Karim Chichakly
- To Marx, Peter,PRT, CPLUS, cplusa
- CC Mark Harley, Tim Schaaff, Leslie Swanson
-
- NewEngland Interoffice Memo
- Digital Reply to: RE>>const vs #define in CFront
- ™
- This is very easy to explain. #define is not at all the same as const.
- #define is a preprocessor directive causing a textual substitution BEFORE the
- compiler scans the code. Therefore, #define symbols do not appear in the
- symbol table.
-
- consts, on the other hand, are compiler statements. You are, in this case,
- generating identifiers that the compiler recognizes and tracks, hence they end
- up in the symbol table. We have observed that storage space is allocated for
- consts (unlike constant #define's), which makes some sense.
-
- These are the disadvantages of const over #define. The advantage, of course,
- is that const's are typed and therefore the compiler can enforce strict
- type-checking when you use them, which is sometimes useful. Note that you can
- effect the same sort of checking in a pinch with #define, by casting the value
- of the constant as in:
-
- #define MAX_SHORT ((short int) 32767) /* maximum short in value */
-
- (also notice the use of /* & */ rather than //, which confuses some
- preprocessors)
-
- --------------------------------------
- Date: 1/23/91
- To: Karim Chichakly
- From: Mark Harley
- Date: 1/23/91
- To: Mark Harley
- From: Marx, Peter,PRT
- Item 4605966 23-Jan-91 00:12PST
-
- From: D4682 Marx, Peter,PRT
-
- To: CPLUS.DEV$ C++ Interest List--Developers
- CPLUS.APPLE$ C++ Interest List--Apple Employees
-
- cc: TOM.CHAVEZ Chavez, Thomas
-
- Sub: const vs #define in CFront
-
- Dear Folks:
-
- This is just an observation that may be useful to someone writing large C++
- programs. After repeatedly running out of memory in CFront using dump/load, I
- decided to try a little experiment.
-
- I replaced most of the "const short int" and "const IDType" declarations with
- simple "#define" statements. The memory and disk space saved was phenomenal. I
- haven't looked at the intermediate code coming out of CFront, but I must say
- that this has turned out to be a very useful "feature."
-
- Perhaps someone could fill me in on why this works so well?
-
- Sincerely,
-
-
- Peter Marx
- UCLA Dept. of Medicine
-
-
-